package qualomelhor.game.entities;

import static playn.core.PlayN.assets;
import static playn.core.PlayN.graphics;
import playn.core.PlayN;
import playn.core.Image;
import playn.core.ImageLayer;
import playn.core.ResourceCallback;
import qualomelhor.game.MyWorld;
import qualomelhor.game.TesteGame;

public abstract class Entity {
	final ImageLayer layer;
	float x, y, angle;

	public Entity(final MyWorld myWorld, float px, float py, float pangle) {
		this.x = px;
		this.y = py;
		this.angle = pangle;
		layer = graphics().createImageLayer(getImage());
		initPreLoad(myWorld);
		getImage().addCallback(new ResourceCallback<Image>() {
			@Override
			public void done(Image image) {
				TesteGame.setMessageBox("imagem carregada em Entity...w,h = " + image.width() + "," + image.height());
				// since the image is loaded, we can use its width and height
				layer.setWidth(image.width());
				layer.setHeight(image.height());
				layer.setOrigin(image.width() / 2f, image.height() / 2f);
				layer.setScale(getWidth() / image.width(), getHeight() / image.height());
				layer.setTranslation(x, y);
				layer.setRotation(angle);
				initPostLoad(myWorld);
			}

			@Override
			public void error(Throwable err) {
				TesteGame.setMessageBox("imagem falhou em Entity...");
				PlayN.log().error("Error loading image: " + err.getMessage());
			}
		});
	}

	/**
   		Perform pre-image load initialization (e.g., attaching to PeaWorld layers).
	 *
	 * @param peaWorld
	 */
	public abstract void initPreLoad(final MyWorld myWorld);

	/**
	* Perform post-image load initialization (e.g., attaching to PeaWorld layers).
	*
	* @param peaWorld
	*/
	public abstract void initPostLoad(final MyWorld myWorld);

	public void paint(float alpha) {
	}

	public void update(float delta) {
	}

	public void setPos(float x, float y) {
		layer.setTranslation(x, y);
	}

	public void setAngle(float a) {
		layer.setRotation(a);
	}

	public abstract float getWidth();

	public abstract float getHeight();

	public abstract Image getImage();

	protected static Image loadImage(String path) {
		return assets().getImage(path);
	}
}